/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ucjava.chat.commserve.rmi.common;

import java.util.prefs.Preferences;

/**
 * Wraps java.util.prefs.Preferences
 *
 * The java.util.prefs.Preferences class takes care of storage location.
 *
 * Accessors in PrefStore return what is stored in the Preferences object. If
 * that object doesn't have what is requested, the default value for that key is
 * returned.
 * 2013-03-24
 * Switched to Singleton-style class
 *
 *
 * @author tony
 */
public class PrefStore
{

     // Initialize this to null so we'll know
    // that factory hasn't already been called.
    private static PrefStore oneAndOnly = null;
    private Preferences prefs;
  
    /////////////////////////////////////////////////////
    //////////////// DEFAULT SETTINGS ///////////////////
    /////////////////////////////////////////////////////
    // Geometry
    private static final int DEFAULT_WINDOW_LEFT = 200;
    private static final int DEFAULT_WINDOW_TOP = 150;
    private static final int DEFAULT_WINDOW_WIDTH = 480;
    private static final int DEFAULT_WINDOW_HEIGHT = 320;
    // Privacy/Personalization
    private static final boolean DEFAULT_AUTOLOGIN = true;
    private static PrivacySettings DEFAULT_PRIVACY = PrivacySettings.OPEN;
    private static final String DEFAULT_USER = "tony";
    private static final int DEFAULT_NUMBER_OF_MSG = 0;
    // Connection
    private static final String DEFAULT_SERVER_IP = "localhost";
    // Menu Labels:
    private static final String DEFAULT_FILE_MNU_TXT = "File";
    private static final String DEFAULT_LOGIN_MNU_TXT = "Log In";
    private static final String DEFAULT_LOGOUT_MNU_TXT = "Log Out";
    private static final String DEFAULT_EXIT_MNU_TXT = "Exit";
    private static final String DEFAULT_EDIT_MNU_TXT = "Edit";
    private static final String DEFAULT_OPTIONS_MNU_TXT = "Options";
    // Other control labels
    private static final String DEFAULT_LOGGED_IN_STATUS = "Logged in as";
    private static final String DEFAULT_LOGGED_OUT_STATUS = "Not logged in";
    private static final String DEFAULT_SEND_BUTTON = "Send";
    private static final String DEFAULT_APP_TITLE = "13SS PROG 2 GROUP PROJECT CLIENT";
    private static final String DEFAULT_NO_MSG_TXT = "No messages yet sent or received by this client";
    private static final String DEFAULT_SEND_HINT = "Type your message and click Send";
    private static final String DEFAULT_BUY_ADS_BTN_TEXT = "Buy Ads";
    private static final String DEFAULT_FRIENDS_LIST_BTN_TEXT = "Friends List";
    // Format strings
    private static final String DEFAULT_TIME_FMT = "HH:mm:ss";
    private static final String DEFAULT_USERNAME_FMT = "%-9.9s";
    // Other strings
    private static final String DEFAULT_LOGIN_REMINDER =
            "You must log in before sending messages.";
    private static final String DEFAULT_LOGIN_REMINDER_TITLE =
            "Not Logged In";
    private static final String DEFAULT_MSG_SEND_FAIL_STRING =
            "Sending of message failed!";
    private static final String DEFAULT_UNAME_PROMPT =
            "Please enter your user name:";
    private static final String DEFAULT_PASSWORD_PROMPT =
            "Please enter your password:";
    private static final String DEFAULT_LOGIN_TITLE =
            "Log In";
    private static final String DEFAULT_ALREADY_LOGGED_IN_MSG =
            "Already logged in";
    private static final String DEFAULT_ALREADY_LOGGED_IN_TITLE =
            "Already logged in";
    /////////////////////////////////////////////////////
    //////////////// END DEFAULT SETTINGS ///////////////
    /////////////////////////////////////////////////////

    //////////////////////////////////////////
    //////////////// ACCESSORS ///////////////
    //////////////////////////////////////////
    public int getWindowLeft()
    {
        return prefs.getInt("windowLeft", this.DEFAULT_WINDOW_LEFT);
    }

    public int getWindowTop()
    {
        return prefs.getInt("windowTop", this.DEFAULT_WINDOW_TOP);
    }

    public int getWindowWidth()
    {
        return prefs.getInt("windowWidth", this.DEFAULT_WINDOW_WIDTH);
    }

    public int getWindowHeight()
    {
        return prefs.getInt("windowHeight", this.DEFAULT_WINDOW_HEIGHT);
    }

    public boolean isAutologin()
    {
        return prefs.getBoolean("autoLogin", this.DEFAULT_AUTOLOGIN);
    }

    /**
     * If the backing store file is corrupt, and the String returned by
     * prefs.get() is not a valid representation of a PrivacySettings value, the
     * call will result in an IllegalArgumentException; in that case, return the
     * default privacy setting.
     *
     * @return
     */
    public PrivacySettings getPrivacy()
    {
        String strPriv = prefs.get("privacy", this.DEFAULT_PRIVACY.toString());
        try
          {
            return PrivacySettings.valueOf(strPriv);
          } catch (IllegalArgumentException e)
          {
            return this.DEFAULT_PRIVACY;
          }
    }

    public int getNumberOfMessagesToShow()
    {
        return prefs.getInt("numMessages", this.DEFAULT_NUMBER_OF_MSG);
    }

    public String getServerIP()
    {
        return prefs.get("serverIP", this.DEFAULT_SERVER_IP);
    }

    public String getUsername()
    {
        return prefs.get("username", this.DEFAULT_USER);
    }

    public String getLoggedInStatusLabel()
    {
        return prefs.get("loggedInStatusLabel", this.DEFAULT_LOGGED_IN_STATUS);
    }

    public String getLoggedOutStatusLabel()
    {
        return prefs.get("loggedOutStatusLabel", this.DEFAULT_LOGGED_OUT_STATUS);
    }

    public String getSendButtonText()
    {
        return prefs.get("sendButtonText", this.DEFAULT_SEND_BUTTON);
    }

    public String getBuyAdsButtonText()
    {
        return prefs.get("buyAdsButtonText", this.DEFAULT_BUY_ADS_BTN_TEXT);
    }
    
    public String getFriendsListButtonText()
    {
        return prefs.get("friendsListButtonText", this.DEFAULT_FRIENDS_LIST_BTN_TEXT);
    }
    
    public String getAppTitle()
    {
        return prefs.get("appTitle", this.DEFAULT_APP_TITLE);
    }

    public String getNoMessagesMessage()
    {
        return prefs.get("noMessagesMessage", this.DEFAULT_NO_MSG_TXT);
    }

    public String getSendHint()
    {
        return prefs.get("sendHint", this.DEFAULT_SEND_HINT);
    }

    public String getFileMenuText()
    {
        return prefs.get("fileMenu", this.DEFAULT_FILE_MNU_TXT);
    }

    public String getFileLoginMenuText()
    {
        return prefs.get("fileLoginMenu", this.DEFAULT_LOGIN_MNU_TXT);
    }

    public String getFileLogoutMenuText()
    {
        return prefs.get("fileLogoutMenu", this.DEFAULT_LOGOUT_MNU_TXT);
    }

    public String getFileExitMenuText()
    {
        return prefs.get("fileExitMenu", this.DEFAULT_EXIT_MNU_TXT);
    }

    public String getEditMenuText()
    {
        return prefs.get("editMenu", this.DEFAULT_EDIT_MNU_TXT);
    }

    public String getEditOptionsMenuText()
    {
        return prefs.get("editOptionsMenu", this.DEFAULT_OPTIONS_MNU_TXT);
    }

    public String getMustLogInReminder()
    {
        return prefs.get("mustLogInReminder", this.DEFAULT_LOGIN_REMINDER);
    }

    public String getMustLogInReminderTitle()
    {
        return prefs.get("mustLogInReminderTitle", this.DEFAULT_LOGIN_REMINDER_TITLE);
    }

    public String getTimeFormat()
    {
        return prefs.get("timeFormat", this.DEFAULT_TIME_FMT);
    }

    public String getUsernameFormat()
    {
        return prefs.get("usernameFormat", this.DEFAULT_USERNAME_FMT);
    }

    public String getSendingFailureMessage()
    {
        return prefs.get("sendingFailureMessage",
                this.DEFAULT_MSG_SEND_FAIL_STRING);
    }

    public String getUsernamePrompt()
    {
        return prefs.get("usernamePrompt", this.DEFAULT_UNAME_PROMPT);
    }
    
    
    public String getPasswordPrompt()
    {
        return prefs.get("passwordPrompt", this.DEFAULT_PASSWORD_PROMPT);
    }
    
    public String getLoginTitle()
    {
        return prefs.get("loginTitle", DEFAULT_LOGIN_TITLE);
    }
    
    public String getAlreadyLoggedInMsg()
    {
        return prefs.get("alreadyLoggedInMsg", this.DEFAULT_ALREADY_LOGGED_IN_MSG);
    }
    
    public String getAlreadyLoggedInTitle()
    {
        return prefs.get("alreadyLoggedInTitle", DEFAULT_ALREADY_LOGGED_IN_TITLE);
    }
    
    //////////////////////////////////////////
    /////////////END OF ACCESSORS ////////////
    //////////////////////////////////////////
    //////////////////////////////////////////
    //////////////// MUTATORS ////////////////
    //////////////////////////////////////////
    public void setWindowLeft(int i)
    {
        prefs.putInt("windowLeft", i);
    }

    public void setWindowTop(int i)
    {
        prefs.putInt("windowTop", i);
    }

    public void setWindowWidth(int i)
    {
        prefs.putInt("windowWidth", i);
    }

    public void setWindowHeight(int i)
    {
        prefs.putInt("windowHeight", i);
    }

    
    /**
     * Whether or not to log in
     * automatically when app is
     * started. For now, this is
     * not used.
     * @param 
     */
    public void setAutologin(boolean b)
    {
        prefs.putBoolean("autoLogin", b);
    }

    public void setPrivacy(PrivacySettings ps)
    {
        prefs.put("privacy", ps.toString());
    }

    public void setNumberOfMessagesToShow(int i)
    {
        prefs.putInt("numMessages", i);
    }

    public void setServerIP(String s)
    {
        prefs.put("serverIP", s);
    }

    public void setUsername(String s)
    {
        prefs.put("username", s);
    }

    public void setLoggedInStatusLabel(String s)
    {
        prefs.put("loggedInStatusLabel", s);
    }

    public void setLoggedOutStatusLabel(String s)
    {
        prefs.put("loggedOutStatusLabel", s);
    }

    public void setSendButtonText(String s)
    {
        prefs.put("sendButtonText", s);
    }

    
    public void setBuyAdsButtonText(String s)
    {
        prefs.put("buyAdsButtonText", s);
    }
    
    public void setFriendsListButtonText(String s)
    {
        prefs.put("friendsListButtonText", s);
    }
    
    public void setAppTitle(String s)
    {
        prefs.put("appTitle", s);
    }

    public void setNoMessagesMessage(String s)
    {
        prefs.put("noMessagesMessage", s);
    }

    public void setSendHint(String s)
    {
        prefs.put("sendHint", s);
    }

    public void setFileMenuText(String s)
    {
        prefs.put("fileMenu", s);
    }

    public void setFileLoginMenuText(String s)
    {
        prefs.put("fileLoginMenu", s);
    }

    public void setFileLogoutMenuText(String s)
    {
        prefs.put("fileLogoutMenu", s);
    }

    public void setFileExitMenuText(String s)
    {
        prefs.put("fileExitMenu", s);
    }

    public void setEditMenuText(String s)
    {
        prefs.put("editMenu", s);
    }

    public void setEditOptionsMenuText(String s)
    {
        prefs.put("editOptionsMenu", s);
    }

    public void setMustLogInReminder(String s)
    {
        prefs.put("mustLogInReminder", s);
    }

    public void setMustLogInReminderTitle(String s)
    {
        prefs.put("mustLogInReminderTitle", s);
    }

    public void setTimeFormat(String s)
    {
        prefs.put("timeFormat", s);
    }

    public void setUsernameFormat(String s)
    {
        prefs.put("usernameFormat", s);
    }

    public void setSendingFailureMessage(String s)
    {
        prefs.put("sendingFailureMessage", s);
    }
    private static final String DEFAULT_CANT_CREATE_CLIENT_MSG = "There was an unknown error while attempting to create the client stub.";

    public String getCantCreateClientMsg()
    {
        return prefs.get("cantCreateClientMsg", DEFAULT_CANT_CREATE_CLIENT_MSG);
    }

    public void setCantCreateClientMsg(String s)
    {
        prefs.put("cantCreateClientMsg", s);
    }
    private static final String DEFAULT_UNSAVED_CHANGES_MSG = "There are unsaved changes. Do you want to save them?";

    public String getUnsavedChangesMsg()
    {
        return prefs.get("unsavedChangesMsg", DEFAULT_UNSAVED_CHANGES_MSG);
    }

    public void setUnsavedChangesMsg(String s)
    {
        prefs.put("unsavedChangesMsg", s);
    }
    private static final String DEFAULT_CANT_CONTACT_SERVER_MSG = "Unable to contact server";

    public String getCantContactServerMsg()
    {
        return prefs.get("cantContactServerMsg", DEFAULT_CANT_CONTACT_SERVER_MSG);
    }

    public void setCantContactServerMsg(String s)
    {
        prefs.put("cantContactServerMsg", s);
    }
    private static final String DEFAULT_CONNECTED_MSG = "Connected to";

    public String getConnectedMsg()
    {
        return prefs.get("connectedMsg", DEFAULT_CONNECTED_MSG);
    }

    public void setConnectedMsg(String s)
    {
        prefs.put("connectedMsg", s);
    }
    private static final String DEFAULT_LOGIN_ERROR_MSG = "Error logging in";

    public String getLoginErrorMsg()
    {
        return prefs.get("loginErrorMsg", DEFAULT_LOGIN_ERROR_MSG);
    }

    public void setLoginErrorMsg(String s)
    {
        prefs.put("loginErrorMsg", s);
    }

    public void setUsernamePrompt(String s)
    {
        prefs.put("usernamePrompt", s);
    }
    
    
    public void setPasswordPrompt(String s)
    {
        prefs.put("passwordPrompt", s);
    }
    
    
    public void setLoginTitle(String s)
    {
        prefs.put("loginTitle", s);
    }
    
    public void setAlreadyLoggedInMsg(String s)
    {
        prefs.put("alreadyLoggedInMsg", s);
    }
  
    public void setAlreadyLoggedInTitle(String s)
    {
        prefs.put("alreadyLoggedInTitle", s);
    }


    //////////////////////////////////////////
    //////////////// END OF MUTATORS /////////
    //////////////////////////////////////////
//    /**
//     * Constructor
//     */
//    public PrefStore()
//    {
//        prefs = Preferences.userNodeForPackage(getClass());
//    } // end constructor
//    
    /**
     * Constructor
     */
    private PrefStore()
    {
        prefs = Preferences.userNodeForPackage(getClass());
    } // end constructor

    /**
     * public method to return reference to one and only instance of this class.
     */
    public static PrefStore makePrefStore()
    {
        if (oneAndOnly == null)
          {
            oneAndOnly = new PrefStore();
          }
        return oneAndOnly;
    }   
    
} // end class PrefStore
